/*
 * @lc app=leetcode.cn id=2765 lang=csharp
 *
 * [2765] 最长交替子数组
 */

// @lc code=start
public class Solution {
    public int AlternatingSubarray(int[] nums) {
        var dp = new int[nums.Length, 2];
        dp[0, 0] = 1;
        dp[0, 1] = 1;
        var max = 1;
        for (int i = 1; i < nums.Length; i++)
        {
            if (nums[i] == nums[i - 1] + 1)
            {
                dp[i, 0] = dp[i - 1, 1] + 1;
                dp[i, 1] = 1;
            }
            else if (nums[i] == nums[i - 1] - 1)
            {
                dp[i, 1] = dp[i - 1, 0] + 1;
                dp[i, 0] = 1;
            }
            else
            {
                dp[i, 0] = 1;
                dp[i, 1] = 1;
            }
            max = Math.Max(max, Math.Max(dp[i, 0], dp[i, 1]));
        }
        return max > 1 ? max : -1;
    }
}
// @lc code=end

